package com.molichuxing.gateway.bff.car.services.Impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.gateway.bff.car.convertor.Convert;
import com.molichuxing.gateway.bff.car.entity.response.CarsViolationVo;
import com.molichuxing.gateway.bff.car.services.ViolationBffService;
import com.molichuxing.gateway.utils.RequestAttrUtil;
import com.molichuxing.services.business.dto.response.UserBizDto;
import com.molichuxing.services.business.service.UserBizService;
import com.molichuxing.services.infrastructure.dto.response.CarsViolationDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.service.CarViolationService;
import com.molichuxing.services.infrastructure.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 14:53 2019/9/11
 * @Modified By:
 **/
@Service("violationBffService")
public class ViolationBffServiceImpl implements ViolationBffService {

    private static final Logger logger = LoggerFactory.getLogger(ViolationBffServiceImpl.class);

    @Resource
    private CarViolationService carViolationService;

    @Resource
    private UserService userService;

    @Resource
    private UserBizService userBizService;

    /**
     * 违章列表
     * @param params
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Paged<CarsViolationVo> getCarsViolationPaged(Map<String, Object> params, Integer pageNum, Integer pageSize) throws Exception {
        Paged<CarsViolationVo> carsViolationVoPage = new Paged<>(null, 0, pageSize);
        //获取当前登录的经销商id
        Integer dealerId = RequestAttrUtil.getCurrentDealerId();
        if(null == dealerId){
            throw new Exception("非法入参");
        }
        //经销商id
        params.put("dealerId",dealerId);

        if(null != params.get("userName")  &&  !"".equals(params.get("userName"))){
            UserDto userDto = userService.getUserByRealName(String.valueOf(params.get("userName")));
            if (null == userDto) {
                params.put("userId",0);
            }else {
                params.put("userId",userDto.getId());
            }
        }

        Paged<CarsViolationDto> carsViolationDtoPage = carViolationService.getCarsViolationPage(params,pageNum,pageSize);
        if (null == carsViolationDtoPage) {
            return carsViolationVoPage;
        }
        BeanUtils.copyProperties(carsViolationDtoPage, carsViolationVoPage);
        if (null == carsViolationDtoPage.getList() || carsViolationDtoPage.getList().size() <= 0) {
            return carsViolationVoPage;
        }

        List<CarsViolationVo> carsViolationVoList = new ArrayList<>();
        CarsViolationVo carsViolationVo = null;
        for (CarsViolationDto carsViolationDto:carsViolationDtoPage.getList()) {
            carsViolationVo =new CarsViolationVo();
            BeanUtils.copyProperties(carsViolationDto, carsViolationVo);
            //获取用户信息
            UserBizDto userBizDto = userBizService.getUser(carsViolationDto.getUserId());
            carsViolationVo.setName(userBizDto.getName());
            carsViolationVo.setRealName(userBizDto.getRealName());
            carsViolationVo.setPhone(userBizDto.getPhone());
            carsViolationVo.setIdNumber(userBizDto.getIdNumber());

            carsViolationVoList.add(carsViolationVo);
        }
        carsViolationVoPage.setList(carsViolationVoList);
        return carsViolationVoPage;
    }

    @Override
    public List<CarsViolationVo> getCarsViolationByCarId(Integer carId) {
        List<CarsViolationDto> beanList = carViolationService.getCarsViolationByCarId(carId);
        return Convert.toCarsViolationVo(beanList, getUserMap(beanList));
    }

    private Map<Integer, UserBizDto> getUserMap(List<CarsViolationDto> beanList) {
        Map<Integer, UserBizDto> result = new HashMap<>();
        if((null != beanList)&&(beanList.size() > 0)) {
            List<Integer> userIds = new ArrayList<>();
            for(CarsViolationDto bean : beanList) {
                userIds.add(bean.getUserId());
            }
            result = userBizService.getUsers(userIds);
        }
        return result;
    }

}
